
using System.Collections;
using Qdb.Tests.Common.Migration;

namespace Qdb.Tests.Common.Migration
{
	public class QdbLibraryEnvironmentProvider
	{
		private readonly IDictionary _environments = new Hashtable();

		private readonly Sharpen.IO.File _classPath;

		public QdbLibraryEnvironmentProvider(Sharpen.IO.File classPath)
		{
			_classPath = classPath;
		}

		/// <exception cref="System.IO.IOException"></exception>
		public virtual QdbLibraryEnvironment EnvironmentFor(string path)
		{
			QdbLibraryEnvironment existing = ExistingEnvironment(path);
			if (existing != null)
			{
				return existing;
			}
			return NewEnvironment(path);
		}

		private QdbLibraryEnvironment ExistingEnvironment(string path)
		{
			return ((QdbLibraryEnvironment)_environments[path]);
		}

		/// <exception cref="System.IO.IOException"></exception>
		private QdbLibraryEnvironment NewEnvironment(string path)
		{
			QdbLibraryEnvironment env = new QdbLibraryEnvironment(new Sharpen.IO.File(path)
				, _classPath);
			_environments[path] = env;
			return env;
		}

		public virtual void DisposeAll()
		{
			for (IEnumerator eIter = _environments.Values.GetEnumerator(); eIter.MoveNext(); )
			{
				QdbLibraryEnvironment e = ((QdbLibraryEnvironment)eIter.Current);
				e.Dispose();
			}
			_environments.Clear();
		}
	}
}
